package com.ThreadImp;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//Thread 实现多线程同步下载图片

public class TestThread2 extends Thread{
    private String url;
    private String name;
    //有参构造
    public TestThread2(String url,String name){
        this.url = url;
        this.name = name;
    }
    //线程体
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downLoader(url,name);
        System.out.println("文件名："+name);
    }
    /**
     *   同时执行,谁先下载完谁显示。
     * */
    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://dianyi.ng/upload/vod/20220329-1/bc6b0a90b1a0c6a9cc1b3acda7e2dfaa.jpg","1.jpg");
        TestThread2 t2 = new TestThread2("https://dianyi.ng/upload/vod/20220603-1/e747260c16af26483e31b30b12ad3951.jpg","2.jpg");
        TestThread2 t3 = new TestThread2("https://dianyi.ng/upload/vod/20220523-1/cd1fe457d23da0404f9e685db6c1e503.jpg","3.jpg");
        //开启线程
        t1.start();
        t2.start();
        t3.start();

    }
}
class WebDownloader{
    public void downLoader(String url,String name){
        try{
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}
